package web2021.team1.lj.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import web2021.team1.lj.dao.LoginLogDao;
import web2021.team1.lj.dao.RegisterDao;
import web2021.team1.lj.dao.UserDao;
import web2021.team1.lj.domain.LoginLog;
import web2021.team1.lj.domain.RegisterLog;
import web2021.team1.lj.domain.User;

import java.util.Date;

/**
 * 切点类
 *
 * @Author imu_zxq
 * @Date 2021/5/22 10:59
 **/

@Aspect
public class AnnotationPointcut {

    @Autowired
    private UserDao userDao;
    @Autowired
    private LoginLogDao loginLogDao;
    @Autowired
    private RegisterDao registerDao;




    @After("execution(* web2021.team1.lj.service.UserService.loginSuccess(web2021.team1.lj.domain.User))")
    public void afterLogin(JoinPoint jp){
        System.out.println("登录成功通知");
        LoginLog loginLog = new LoginLog();
        Object[] usetTemp = jp.getArgs();
        User user = (User) usetTemp[0];
        loginLog.setUserId(user.getUserId());
        loginLog.setIp("127.0.0.2");
        loginLog.setLoginDate(new Date());
        loginLogDao.insertLoginLog(loginLog);
        userDao.updateLoginInfo(user);
    }

    @After("execution(* web2021.team1.lj.service.UserService.registerUser(String, String))")
    public void afterRegist(JoinPoint jp){
        System.out.println("注册功能运行结束通知");
        RegisterLog registerLog = new RegisterLog();
        Object[] registerlogTemp = jp.getArgs();
        for (Object item: registerlogTemp) {
            System.out.println(item);
        }
//        User user = (User) registerlogTemp[0];
        registerLog.setUserName(registerlogTemp[0].toString());
        registerLog.setIp("127.0.0.1");
        registerLog.setRegisterDate(new Date());
        registerDao.insertRegisterLog(registerLog);
    }
}
